package com.zhengb.flink.study.sql.batchsql;

import com.zhengb.flink.study.sql.model.WC;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.java.BatchTableEnvironment;


/**
 * Created by zhengbo on 2019/12/31.
 */
public class DataSetWordCountSql {

    public static void main(String[] args) throws Exception {


        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        BatchTableEnvironment tableEnvironment = BatchTableEnvironment.create(env);

        DataSource<WC> input = env.fromElements(
                new WC("zhengbo", 1),
                new WC("zhangyi", 1),
                new WC("zhangyi", 2));

        tableEnvironment.registerDataSet("wordCountDataSet", input, "word, count");

        Table table = tableEnvironment.sqlQuery("SELECT word,SUM(count) as count from wordCountDataSet GROUP BY word");

        tableEnvironment.toDataSet(table, WC.class).print();

    }
}
